package com.yangcsoft.modules.deptmanage.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.yangcsoft.common.utils.DateUtils;
import com.yangcsoft.common.utils.ExcelUtil;
import com.yangcsoft.common.utils.PageUtils;
import com.yangcsoft.common.utils.R;
import com.yangcsoft.common.validator.ValidatorUtils;
import com.yangcsoft.modules.deptmanage.entity.WeeklyInfoEntity;
import com.yangcsoft.modules.deptmanage.service.WeeklyInfoService;
import com.yangcsoft.modules.sys.shiro.ShiroUtils;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;


/**
 * 周报信息表
 *
 * @author XueQing Wang
 * @email 17621152225@163.com
 * @date 2019-05-02 14:22:44
 */
@RestController
@RequestMapping("deptmanage/weeklyinfo")
public class WeeklyInfoController {
    @Autowired
    private WeeklyInfoService weeklyInfoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("deptmanage:weeklyinfo:list")
    public R list(@RequestParam Map<String, Object> params) {
        String key = "";
        Object keyObj = params.get("key");
        if (keyObj != null) {
            key = keyObj.toString();
        }
        params.put("key", key);
        PageUtils page = weeklyInfoService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{weeklyNo}")
    @RequiresPermissions("deptmanage:weeklyinfo:info")
    public R info(@PathVariable("weeklyNo") Integer weeklyNo) {
        WeeklyInfoEntity weeklyInfo = weeklyInfoService.selectById(weeklyNo);

        return R.ok().put("weeklyInfo", weeklyInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("deptmanage:weeklyinfo:save")
    public R save(@RequestBody WeeklyInfoEntity weeklyInfo) {
        weeklyInfo.setCreateDate(DateUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
        weeklyInfo.setCreateUser(ShiroUtils.getUserEntity().getUsername());
        weeklyInfoService.save(weeklyInfo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("deptmanage:weeklyinfo:update")
    public R update(@RequestBody WeeklyInfoEntity weeklyInfo) {
        ValidatorUtils.validateEntity(weeklyInfo);
        weeklyInfo.setUpdateDate(DateUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
        weeklyInfo.setUpdateUser(ShiroUtils.getUserEntity().getUsername());
        weeklyInfoService.updateAllColumnById(weeklyInfo);//全部更新

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("deptmanage:weeklyinfo:delete")
    public R delete(@RequestBody Integer[] weeklyNos) {
        weeklyInfoService.deleteBatchIds(Arrays.asList(weeklyNos));

        return R.ok();
    }

    /**
     * 导出报表
     *
     * @param response
     * @return
     */
    @RequestMapping("/export")
    public R export(Long[] ids, HttpServletResponse response, HttpServletRequest request) {
        String[] secondTitles = null;
        String[] beanProperty = null;
        String title = null;
        secondTitles = new String[]{
                "周报序号",
                "人员序号",
                "本周周报",
                "下周周报",
                "周报开始日期",
                "周报结束日期",
                "创建日期",
                "创建人",
                "修改日期",
                "修改人"
        };
        beanProperty = new String[]{
                "weeklyNo",
                "userseq",
                "nowWeekly",
                "nextWeekly",
                "weeklyStartDate",
                "weeklyEndDate",
                "createDate",
                "createUser",
                "updateDate",
                "updateUser"
        };
        title = "周报信息表报表";

        List<WeeklyInfoEntity> list = weeklyInfoService.selectList(new EntityWrapper<>());


        File file = ExcelUtil.exportResult(request, list, title, secondTitles, beanProperty);
        try {
            response.setContentType("application/x-download");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(title.getBytes("gbk"), "iso8859-1") + ".xls");
            ServletOutputStream outputStream = response.getOutputStream();
            FileInputStream fileInputStream = new FileInputStream(file);
            IOUtils.copy(fileInputStream, outputStream);
            outputStream.close();
            fileInputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.ok();
    }

    /**
     * 导入报表
     *
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/batchImport")
    public R batchImport(HttpServletResponse response, MultipartFile file) throws Exception {
        String[] beanProperty = null;
        beanProperty = new String[]{
                "userseq",
                "nowWeekly",
                "nextWeekly",
                "weeklyStartDate",
                "weeklyEndDate"
        };
        List<WeeklyInfoEntity> list = ExcelUtil.parserExcel(WeeklyInfoEntity.class, file, beanProperty);
        for (WeeklyInfoEntity entity : list) {
            save(entity);
        }
        return R.ok();
    }

    /**
     * 下载导入模版
     */
    @RequestMapping("/templateDownload")
    public R exportXls(HttpServletResponse response) {
        String title = "周报信息表录入模版";
        String[] secondTitles = new String[]{
                "人员序号",
                "本周周报",
                "下周周报",
                "周报开始日期",
                "周报结束日期"
        };

        Integer startRow = 0;
        Integer endRow = 0;
        Integer startCol = 0;
        Integer endCol = 0;

        ExcelUtil.templateDownload(response, title, title, secondTitles, new ArrayList<>(), startRow, endRow, startCol, endCol);

        return R.ok();
    }

}
